package com.woniuxy.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.coupon.client.CouponQueryClient;
import com.woniuxy.coupon.client.result.CouponDto;
import com.woniuxy.food.client.foodskuclient.FoodSkuQueryClient;
import com.woniuxy.food.client.foodspuclient.FoodSpuQueryClient;
import com.woniuxy.food.client.result.FoodSpuClientDto;
import com.woniuxy.member.infra.mapper.ExchangeCouponMapper;
import com.woniuxy.member.infra.mapper.ExchangeFoodMapper;
import com.woniuxy.member.infra.model.ExchangeCoupon;
import com.woniuxy.member.infra.model.ExchangeFood;
import com.woniuxy.member.service.ExchangeFoodService;
import com.woniuxy.member.service.dto.ExchangeCouponDto;
import com.woniuxy.member.service.dto.ExchangeFoodDto;
import com.woniuxy.member.service.param.ExchangeCouponSearchServiceParam;
import com.woniuxy.member.service.param.ExchangeFoodSearchServiceParam;
import common.WoniuPage;
import org.aspectj.weaver.patterns.IfPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 兑换菜品表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class ExchangeFoodServiceImpl extends ServiceImpl<ExchangeFoodMapper, ExchangeFood> implements ExchangeFoodService {
    @Autowired
    private ExchangeFoodMapper exchangeFoodMapper;
    @Autowired
    private ExchangeCouponMapper exchangeCouponMapper;
    @Autowired
    private FoodSpuQueryClient foodSpuQueryClient;
    @Autowired
    private CouponQueryClient couponQueryClient;
    @Override
    public WoniuPage<ExchangeFoodDto> getScoreExchangeFood(ExchangeFoodSearchServiceParam exchangeFoodSearchServiceParam) {

        QueryWrapper<ExchangeFood> queryWrapper = new QueryWrapper<>();
        if (exchangeFoodSearchServiceParam.getMinScore()!=null){
            queryWrapper.ge("score",exchangeFoodSearchServiceParam.getMinScore());
        }
        if (exchangeFoodSearchServiceParam.getMaxScore()!=null){
            queryWrapper.le("score",exchangeFoodSearchServiceParam.getMaxScore());
        }
        if (exchangeFoodSearchServiceParam.getSkuName()!=null){
            queryWrapper.like("sku_name",exchangeFoodSearchServiceParam.getSkuName());
        }
        Page<ExchangeFood> exchangeFoodPage = new Page<>(exchangeFoodSearchServiceParam.getPageIndex(), exchangeFoodSearchServiceParam.getPageSize());
        exchangeFoodMapper.selectPage(exchangeFoodPage,queryWrapper);
        WoniuPage<ExchangeFoodDto> woniuPage = new WoniuPage<ExchangeFoodDto>();
        woniuPage.setTotal(exchangeFoodPage.getTotal());
        woniuPage.setPages(exchangeFoodPage.getPages());
        woniuPage.setPageIndex(exchangeFoodPage.getCurrent());
        woniuPage.setPageSize(exchangeFoodPage.getSize());
        ArrayList<ExchangeFoodDto> list = new ArrayList<>();
        for (ExchangeFood exchangeFood : exchangeFoodPage.getRecords()) {
            ExchangeFoodDto exchangeFoodDto = BeanUtil.toBean(exchangeFood, ExchangeFoodDto.class);
            Result<FoodSpuClientDto> foodSpuClientDtoResult = foodSpuQueryClient.searchFoodSpuBySkuId(exchangeFoodDto.getSkuId());
            if (foodSpuClientDtoResult.getData().getStoreId()==exchangeFoodSearchServiceParam.getStoreId()) {
                list.add(exchangeFoodDto);
            }
        }
        woniuPage.setList(list);
        return woniuPage;
    }

    @Override
    public List<ExchangeCouponDto> getScoreExchangeCoupon(ExchangeCouponSearchServiceParam exchangeCouponSearchServiceParam) {
        QueryWrapper<ExchangeCoupon> queryWrapper = new QueryWrapper<>();
        if (exchangeCouponSearchServiceParam.getMinScore()!=null){
            queryWrapper.ge("score",exchangeCouponSearchServiceParam.getMinScore());
        }
        if (exchangeCouponSearchServiceParam.getMaxScore()!=null){
            queryWrapper.le("score",exchangeCouponSearchServiceParam.getMaxScore());
        }
        if (exchangeCouponSearchServiceParam.getCouponName()!=null){
            queryWrapper.like("coupon_name",exchangeCouponSearchServiceParam.getCouponName());
        }
        List<ExchangeCoupon> exchangeCoupons = exchangeCouponMapper.selectList(queryWrapper);
        List<ExchangeCoupon> exchangeCoupons1 = new ArrayList<>();
        for (ExchangeCoupon exchangeCoupon : exchangeCoupons) {
            Result<CouponDto> couponDtoResult = couponQueryClient.queryCouponByCouponId(exchangeCoupon.getCouponId());
            if (couponDtoResult.getData().getStoreId()==exchangeCouponSearchServiceParam.getStoreId()) {
                exchangeCoupons1.add(exchangeCoupon);
            }

        }
        List<ExchangeCouponDto> exchangeCouponDtos = BeanUtil.copyToList(exchangeCoupons1, ExchangeCouponDto.class);
        return exchangeCouponDtos;
    }
}
